/*
gobanjs - html5 SGF viewer for the Game of Go 

Copyright (C) 2013  Daniel Cioata
Visit http://www.govortex.ro/ for documentation, updates and examples.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/    

"use strict";

var GobanJS={
    "clist":null    
};

function createDiagrams(className)
{
    var dia_list,i,pre_tag,controller;
    
    dia_list = document.getElementsByClassName(className);
    if(dia_list==null)
        return;
        
    GobanJS.clist=new Array();    
    for(i=0;i<dia_list.length;i++)
    {
        pre_tag=dia_list[i].getElementsByTagName('pre')[0];  
        controller=new GController();
        controller.init(dia_list[i],pre_tag.innerHTML);
        GobanJS.clist.push(controller);
    }
}

function handleEvent(evt)
{
    var cmd,ind,obj,varnr;       
    varnr=-1;
    cmd=evt.target.id.split("-");   
    ind=parseInt(cmd[1]);        
    obj=GobanJS.clist[ind];
    if(cmd[0]=="sgo_var")
        varnr=parseInt(cmd[2]);    
    obj.model.handleCommand(cmd[0],varnr);            
    console.log("varnr",varnr);
    obj.view.update();
    evt.preventDefault();
    evt.stopPropagation();       
}   

function GController()
{      
    this.index;
    this.model=null;
    this.view=null;
    this.config={ "autocrop":false, "commentpos":"bottom", 
                  "filetype":"game", "maxwidth":480,
                  "maxvar":10, "boardsize":19,"shownext":true,
                  "showvars":true
                 };   
                                 
    this.init=function(parent_div,rawsgf)
    {        
        
        this.index=GobanJS.clist.length;        
        this.model=new Model(this, this.index);
        this.view=new View(this, this.index);        
        this.updateConfig(rawsgf);        
        this.model.init(rawsgf);
        this.view.init(parent_div);                
        this.view.update();
    }
    
    this.updateConfig = function(rawsgf)
    {
        var jstr,cfg;
        jstr=rawsgf.match(/\{.*?\}/);
        
        if(jstr!=null)        
        {
            try {cfg = JSON.parse(jstr);}
            catch(e) { console.error("Parsing error:", e); return;}
        }
        else
            return;

        if(cfg.commentpos!=undefined)
        {
            this.config.commentpos=cfg.commentpos;
        }
        if(cfg.filetype!=undefined)
        {
            this.config.filetype=cfg.filetype;                            
        }
        if(cfg.autocrop!=undefined)
        {
             this.config.autocrop=cfg.autocrop;
        }
        if(cfg.shownext!=undefined)
        {
            this.config.shownext=cfg.shownext;            
        }
        if(cfg.showvars!=undefined)
        {
            this.config.showvars=cfg.showvars;            
        }        
        if(this.config.filetype=="problem")
        {
            this.config.showvar=false;            
        }
                
    }
}

window.onload = function()
{   
    createDiagrams('sgodia');
}

